package com.miaoxun.dao.provider;

import com.miaoxun.dao.util.ClassUtil;
import com.miaoxun.domain.entity.User;
import com.miaoxun.domain.model.UserModel;
import com.miaoxun.domain.param.UserParam;
import org.apache.ibatis.jdbc.SQL;

public class UserProvider {
    public String listByDept(UserParam param) {

        SQL sql = new SQL();
        sql.SELECT(ClassUtil.field2comun(User.class));
        sql.FROM("user");
        sql.WHERE("1=1");
        if (param != null && param.getDeptId() > 0) {
            sql.AND();
            sql.WHERE("dept_id=" + param.getDeptId());
        }
        if (param.getName() != null && !"".equalsIgnoreCase(param.getName())) {
            sql.AND();
            sql.WHERE("name like '%" + param.getName() + "%'");
        }

        return sql.toString();
    }

    public String findByUsername(String name) {
        SQL sql = new SQL();
        sql.SELECT(ClassUtil.field2comun(User.class));
        sql.FROM("user");
        sql.WHERE("1=1");
        sql.AND().WHERE("username='" + name.trim() + "'");
        return sql.toString();
    }

    public String findUserDeptById(Long id) {
        SQL sql=new SQL();
        sql.SELECT(ClassUtil.field2comun(UserModel.class));
        sql.FROM("user");
        sql.INNER_JOIN("dept on dept.dept_id=user.dept_id");
        sql.WHERE("1=1");
        sql.AND().WHERE("user_id="+id);
        return sql.toString();
    }
}
